// -*- c++ -*-
/**************************************************************/
/*  This file is licensed from, and is a trade secret of:     */
/*                                                            */
/*    Mercury Computer Systems, Incorporated                  */
/*    199 Riverneck Road                                      */
/*    Chelmsford, Massachusetts 01824-2820                    */
/*    United States of America                                */
/*    Telephone + 1 978 256-1300                              */
/*                                                            */
/*  Refer to your Software License Agreement for provisions   */
/*  on use, duplication, or disclosure.                       */
/*  US. Government users and prime contractors are also       */
/*  governed by the following Data Rights Notice:             */
/*  Use, duplication or disclosure by the US. Government is   */
/*  subject to the provisions set forth in FAR 52.227-14 for  */
/*  restricted rights or other agency data rights provisions  */
/*  on commercial software and documentation, as applicable.  */
/*                                                            */
/*  This file: (a) was developed at private expense, and no   */
/*  part was developed with government funds, (b) is a        */
/*  trade secret of Mercury Computer Systems, Inc. for the    */
/*  purposes of the Freedom of Information Act, (c) is        */
/*  "commercial computer software" subject to limited         */
/*  utilization as provided in the above noted Software       */
/*  License Agreement and (d) is in all respects data         */
/*  belonging exclusively to Mercury Computer Systems, Inc..  */
/*                                                            */
/**************************************************************/

/******************************************************************/
/* Copyright (c) 1999-2003, Mercury Computer Systems, Inc.,       */
/* Chelmsford MA.,and all third party embedded software sources.  */
/* All rights reserved under the Copyright laws of US. and        */
/* international treaties.                                        */
/*                                                                */
/* Patent Pending                                                 */
/******************************************************************/


//$COPYRIGHT_END


#include <SceOsAssert.h>
#include <SceOsLoadableModule.h>
#include <SceOsSizeCheck.h>
#include <SceOsDataTypes.h>
#include <cassert>
#include <string>
#include <windows.h>
#include "SceOsWin32Error.h"

inline
HMODULE &
o2h (SCE::OS::uint64_t * ptr)
  throw ()
{
  return *reinterpret_cast<HMODULE *> (ptr);
}

SCE::OS::LoadableModule::LoadableModule ()
  throw ()
{
  sceAssert ((compileTimeSizeCheck<sizeof (m_osOpaque), sizeof (HMODULE)> ()));
  sceAssert (sizeof (m_osOpaque) >= sizeof (HMODULE));
#if !defined(NDEBUG)
  o2h (m_osOpaque) = 0;
#endif
}

SCE::OS::LoadableModule::LoadableModule (const std::string & fileName)
  throw (std::string)
{
  sceAssert ((compileTimeSizeCheck<sizeof (m_osOpaque), sizeof (HMODULE)> ()));
  sceAssert (sizeof (m_osOpaque) >= sizeof (HMODULE));
#if !defined(NDEBUG)
  o2h (m_osOpaque) = 0;
#endif
  open (fileName);
}

SCE::OS::LoadableModule::~LoadableModule ()
  throw ()
{
#if !defined(NDEBUG)
  sceAssert (!o2h (m_osOpaque));
#endif
}

void
SCE::OS::LoadableModule::open (const std::string & fileName)
  throw (std::string)
{
  o2h (m_osOpaque) = LoadLibrary (fileName.c_str());
  if (!(o2h (m_osOpaque))) {
    throw SCE::OS::Win32::getErrorMessage (GetLastError());
  }
}

void *
SCE::OS::LoadableModule::getSymbol (const std::string & functionName)
{
#if !defined(NDEBUG)
  sceAssert (o2h (m_osOpaque));
#endif

  void * addr;

  addr = GetProcAddress (o2h (m_osOpaque), functionName.c_str());
  if (addr == NULL) {
    throw SCE::OS::Win32::getErrorMessage (GetLastError());
  }

  return addr;
}

void
SCE::OS::LoadableModule::close ()
  throw (std::string)
{
#if !defined(NDEBUG)
  sceAssert (o2h (m_osOpaque));
#endif

  if (!FreeLibrary (o2h (m_osOpaque))) {
    throw SCE::OS::Win32::getErrorMessage (GetLastError());
  }

#if !defined(NDEBUG)
  o2h (m_osOpaque) = 0;
#endif
}
